package cn.tj.controller;

import cn.tj.entity.Food;
import cn.tj.entity.PageBean;
import cn.tj.entity.Result;
import cn.tj.service.FoodService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/food")
public class FoodController {
    @Autowired
    private FoodService foodService;

    /**
     * 条件分页查询食物列表
     * @param currentPage 当前页
     * @param pageSize 每页数据条数
     * @param minPrice 最小价格
     * @param maxPrice 最大价格
     * @return
     */
    @GetMapping("/foodListByPage")
    public Result<PageBean> foodListByPage(
            Integer currentPage,
            Integer pageSize,
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice
    ){
       PageBean pg = foodService.foodListByPage(currentPage,pageSize,minPrice,maxPrice);
       return Result.success(pg);
    }

    /**
     * 通过id获取食物信息
     * @param id
     * @return
     */
    @GetMapping("/getFoodById")
    public Result<Food> getFoodById(Integer id){
        Food food = foodService.getFoodById(id);
        return Result.success(food);
    }

    /**
     * 修改食物信息
     * @param food
     * @return
     */
    @PutMapping("/editFood")
    public Result editFood(@RequestBody Food food){
        foodService.editFood(food);
        return Result.success();
    }

    /**
     * 通过id删除食物信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteFoodById")
    public Result deleteFoodById(Integer id){
        foodService.deleteFoodById(id);
        return Result.success();
    }

    /**
     * 新增食物
     * @param food
     * @return
     */
    @PostMapping("/addFood")
    public Result addFood(@RequestBody Food food){
        foodService.addFood(food);
        return Result.success();
    }
}
